﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.Model;


namespace Besture.UI.Admin.Content
{
    public partial class WebOptionsSet : BasePage
    {

        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            //页面加载
            InitPage();
            //编辑编号
            InitData(ID);
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 页面加载
        /// </summary>
        public void InitPage()
        {
            
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            if (id <= 0)
            {
                return;
            }
            WebOptionsDac dac = new WebOptionsDac();
            WebOptionsModel model = dac.GetModel(id);
            txtName.Text = model.Name;
            txtValue.Text = model.Value;

        }
        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            WebOptionsDac dac = new WebOptionsDac();
            WebOptionsModel model = null;
            //添加
            if (!EditPage)
            {
                model = new WebOptionsModel();
                model.Name = txtName.Text;
                model.Value = txtValue.Text;
                model.Guid = Guid.NewGuid().ToString();
                dac.Add(model);
            }
            //编辑
            else
            {
                model = dac.GetModel(ID);
                model.Name = txtName.Text;
                model.Value = txtValue.Text;
                dac.Update(model);
            }


            switch (model.Guid)
            {
                case "6dfc8b52-e997-4054-b0f5-e18c078efdcd":
                    Generate();
                    break;
                case "7ded9893-a089-4cac-9376-9143c8112388":
                    Generate();
                    break;
            }


            FunHelper.Alert("保存成功", "WebOptionsList.aspx");
        }
        #endregion

        #region 取消

        public void btnCanel_Click(object sender, EventArgs eventArgs)
        {
            RedirectUrl("WebOptionsList.aspx");
        }

        #endregion

        public void Generate()
        {
            GenerateHtml generateHtml = new GenerateHtml();
            WebOptionsDac dac = new WebOptionsDac();
            Dictionary<string,string> contact=new Dictionary<string, string>();
            WebOptionsModel email = dac.GetModel("6dfc8b52-e997-4054-b0f5-e18c078efdcd");
            WebOptionsModel phone = dac.GetModel("6dfc8b52-e997-4054-b0f5-e18c078efdcd");
            contact.Add("[ClientPhone]", phone.Value);
            contact.Add("[ClientEmail]", email.Value);
            generateHtml.Universal(Server.MapPath("/Templates/solutions/company.shtml"), Server.MapPath("/solutions/Module/company.shtml"), contact);
        }
    }
}